package net.shopxx.action;

public class Test {

	Integer no = 5;
	boolean f1 = true;
	boolean f2 = true;
	boolean f3 = true;

	class T1 implements Runnable {
		public void run() {
			while (true) {
				synchronized (no) {
					if (no <= 0 || !f1) {
						try {
							System.out.println("t111111111 wait");
							f1 = false;
							no.wait();
							System.out.println("被唤醒t1111111111111");
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					} 
					if(no > 0){
						no--;
					}
					System.out.println("t11111111111---"+no);
					
				}
				
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	class T2 implements Runnable {
		public void run() {
			while (true) {
				synchronized (no) {
					if (no >= 10 || !f2) {
						try {
							System.out.println("t222222222222 wait");
							f2 = false;
							no.wait();
							System.out.println("被唤醒t222222222222");
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
					if(no < 10){
						no++;
					}
					System.out.println("t22222222222---"+no);
					
				}
				
				try {
					Thread.sleep(1500);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	class T3 implements Runnable {
		public void run() {
			while (true) {
				synchronized (no) {
					if (no >= 10 || !f3) {
						try {
							System.out.println("t3333333333333333 wait");
							f3 = false;
							no.wait();
							System.out.println("被唤醒t333333333333333");
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
					if(no < 10){
						no++;
					}
					System.out.println("t33333333333---"+no);
					
				}
				
				try {
					Thread.sleep(1500);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	class T4 implements Runnable {
		Thread t1;
		Thread t2;
		Thread t3;
		public T4(Thread t1,Thread t2,Thread t3){
			this.t1 = t1;
			this.t2 = t2;
			this.t3 = t3;
		}
		
		public void run() {
			while (true) {
				synchronized (no) {
					if(no <= 0){
						f2 = true;
						f3 = true;
						System.out.println("----------------------------唤醒2222222222222222--33333333");
					}
					if(no >= 10){
						f1 = true;
						System.out.println("--------------------------唤醒11111111111111111 ");
					}
					no.notifyAll();
				}
				try {
					Thread.sleep(500);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	private void start(){
		Thread t1 = new Thread(new T1());
		Thread t2 = new Thread(new T2());
		Thread t3 = new Thread(new T3());
		Thread t4 = new Thread(new T4(t1,t2,t3));
		t1.setName("t1");
		t2.setName("t2");
		t3.setName("t3");
		t4.setName("t4");
		t1.start();
		t2.start();
		t3.start();
		t4.start();
	}
	
	public static void main(String[] args) {
		new Test().start();
	}
}
